﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS_Server;
using SMS_Server_Manager;

namespace SMS_Server_Manager.Forms
{
    public partial class SmsEmail : Form
    {
        public SmsEmail()
        {
            InitializeComponent();
        }

        private Action_SMS_Email smsemail;

        public Action_SMS_Email SMS_Email
        {
            get { return smsemail; }
            set
            {
                smsemail = value;
                FunctionName.Text = smsemail.ActionName;
                ParamFormat.Text = smsemail.ActionFormat;
                if (smsemail.Mode == Action_SMS_Email.SendingMode.SMS) SMS.Checked = true; else Email.Checked = true;
                Address.Text = smsemail.Address;
            }
        }

        private Form Sender;

        public System.Windows.Forms.DialogResult ViewDialog(Form sender)
        {
            Sender = sender;
            return this.ShowDialog(sender);
        }

        private void CancleButton_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            if (Email.Checked == true)
            {
                if (!validators.IsValidEmail(Address.Text))
                {
                    MessageBox.Show("Invalid e-mail address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                if (!validators.IsValidPhoneNumber(Address.Text))
                {
                    MessageBox.Show("A valid phone number with country code is required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

            }
            //lets save the staticResponse now
            if (SMS.Checked) smsemail.Mode = Action_SMS_Email.SendingMode.SMS; else smsemail.Mode = Action_SMS_Email.SendingMode.EMAIL;
            smsemail.Address = Address.Text;
            DataAccess DA = new DataAccess(Global.GetConfigurationDatabaseName(), Global.DatabasePassword);
            if (smsemail.actionID == 0)
            {
                DA.CommandString = "INSERT INTO Action_SMS_Email (FunctionName, Format, Address, Mode) VALUES (@FunctionName, @Format, @Address, @Mode);";
                DA.AddParameter("@FunctionName", smsemail.ActionName);
                DA.AddParameter("@Format", smsemail.ActionFormat);
                DA.AddParameter("@Address", smsemail.Address);
                DA.AddParameter("@Mode", smsemail.Mode.ToString());
                DA.ExecuteNonQuery();
                DA.CommandString = "SELECT ActionID FROM Action_SMS_Email WHERE FunctionName=@FunctionName AND Format=@Format AND Address=@Address AND Mode=@Mode;";
                DA.ExecuteReader();
                while (DA.Reader.Read())
                {
                    int ActionID = (int)DA.Reader.GetInt64(0);
                    bool ActionExist = false;
                    foreach (Incoming_Action ia in ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions)
                    {
                        if (ia.ActionID() == (long)ActionID && ia.ActionType() == ActionTypes.StaticResponse)
                        {
                            ActionExist = true;
                        }
                    }
                    if (!ActionExist)
                    {
                        smsemail.actionID = ActionID;
                        ((Function)((Mainform)Sender).FunctionList.SelectedItem).Actions.Add(smsemail);
                        break;
                    }
                }
                DA.Reader.Close();
                DA.Close();
                ((Mainform)Sender).ActionList.Items.Add(smsemail);
            }
            else
            {
                DA.CommandString = "UPDATE Action_SMS_Email SET Address=@Address, Mode=@Mode WHERE ActionID=@ActionID;";
                DA.AddParameter("@Address", smsemail.Address);
                DA.AddParameter("@Mode", smsemail.Mode.ToString());
                DA.AddParameter("@ActionID", smsemail.actionID);
                DA.ExecuteNonQuery();
                DA.Close();
            }
            Global.unsaved_changes = true;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
